SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE Procedure [dbo].[SP_TMDOCU_CLIE_I07]        
/*-------------------------------------------------------*/        
/*---Empresa              : OFISIS S.A.                --*/        
/*---Cliente              : OFISIS                 --*/        
/*---Sistema              : Tesoreria                  --*/        
/*---M©dulo               : Empresa                    --*/        
/*---Programa             : Grabaci©n Doc.Clientes     --*/        
/*---                       M®dulo Tesoreria y Ventas  --*/        
/*---Script               : tcmovi_b.sql               --*/        
/*---Nombre SP            : SP_TMDOCU_CLIE_I02         --*/        
/*---Desarrollado por     : Javier Flores A.           --*/        
/*---Fecha Creaci©n       : 14/03/2000                 --*/        
/*---Base Datos           : Microsoft Sql Server       --*/        
/*---Versi©n              : 7.0                        --*/        
/*---Invoca a SP          :                            --*/        
/*-------------------------------------------------------*/        
        
/*-------------------------------------------------------*/        
/*---Modificado 1 por     : Maribel Julca Rodriguez    --*/        
/*---Fecha Modificaci©n   : 21/04/2005                 --*/         
/*---Detalle Modificaci©n : ingreso de los campos CO_USUA_CREA, CO_USUA_MODI  --*/        
/*-------------------------------------------------------*/        
        
/*-------------------------------------------------------*/        
/*---Comentarios                                       --*/        
/*---                                                  --*/        
/*---                                                  --*/        
/*-------------------------------------------------------*/        
        
/*-------------------------------------------------------*/        
/*---Drop Proc SP_TMDOCU_CLIE_I02                      --*/        
/*---Grant Execute on SP_TMDOCU_CLIE_I02 to public     --*/         
/*---SP_TMDOCU_CLIE_I02                                --*/        
/*-------------------------------------------------------*/        
   
 As      
DECLARE         
@VSTI_CLIE  TD_VC_003,         
@VSNU_ASTO  TD_VC_020,        
@VSST_INTE_BACH TD_VC_001,      
@VSCO_TIPO_OPER TD_VC_003,      
@VSCO_TIPO_OPEG TD_VC_003,  
  
 @ISCO_EMPR      TD_VC_002,         
 @ISCO_USUA_MODI TD_VC_008_USUA,        
 @ISCO_TIPO_DOCU TD_VC_003,         
 @ISNU_DOCU_CLIE TD_VC_020,         
 @ISCO_CLIE      TD_VC_020,        
 @ISNO_CLIE      TD_VC_100,        
 @ISCO_MONE      TD_VC_003,         
 @ISCO_TIPO_OPER TD_VC_003,         
 @ISCO_COND_PAGO TD_VC_003,          
 @IDFE_EMIS      TD_DT_001,         
 @ISCO_UNID_CONC TD_VC_003,         
 @IDFE_VENC      TD_DT_001,        
 @IDFE_PROG_COBR TD_DT_001,        
 @IDFE_DESP      TD_DT_001,        
 @INFA_TIPO_CAMB TD_NU_015_006,         
 @INIM_BRUT_AFEC TD_NU_016_004,        
 @INIM_BRUT_INAF TD_NU_016_004,    
 @INIM_BRUT_EXON TD_NU_016_004,              
 @INIM_GAFI      TD_NU_016_004,         
 @INIM_MORA      TD_NU_016_004,         
 @INIM_FLET      TD_NU_016_004,        
 @ISST_AFEC_GAFI TD_VC_001,         
 @ISST_AFEC_MORA TD_VC_001,         
 @ISST_AFEC_FLET TD_VC_001,         
 @ISST_DCT1_BRAF TD_VC_001,        
 @ISST_DCT1_BRIN TD_VC_001,    
 @ISST_DCT1_BREX TD_VC_001,              
 @ISST_DCT1_GAFI TD_VC_001,         
 @ISST_DCT1_MORA TD_VC_001,         
 @ISST_DCT1_FLET TD_VC_001,        
 @ISST_DCT2_BRAF TD_VC_001,         
 @ISST_DCT2_BRIN TD_VC_001,    
 @ISST_DCT2_BREX TD_VC_001,              
 @ISST_DCT2_GAFI TD_VC_001,         
 @ISST_DCT2_MORA TD_VC_001,        
 @ISST_DCT2_FLET TD_VC_001,         
 @INPC_DCT1      TD_NU_009_004,         
 @INIM_DCT1      TD_NU_016_004,         
 @INPC_DCT2      TD_NU_009_004,        
 @INIM_DCT2      TD_NU_016_004,          
 @ISCO_IMP1      TD_VC_003,         
 @INPC_IMP1      TD_NU_009_004,         
 @INIM_IMP1      TD_NU_016_004,         
 @ISCO_IMP2      TD_VC_003,        
 @INPC_IMP2      TD_NU_009_004,         
 @INIM_IMP2      TD_NU_016_004,         
@ISCO_IMP3      TD_VC_003,         
 @INPC_IMP3      TD_NU_009_004,         
 @INIM_IMP3      TD_NU_016_004,        
 @INIM_TOTA      TD_NU_016_004,         
 @INIM_PAGA_REND TD_NU_016_004,         
 @ISCO_TIDO_ORIG TD_VC_003,         
 @ISNU_DOCU_ORIG TD_VC_020,        
 @IDFE_DOCU_ORIG TD_DT_001,         
 @ISDE_OBSE      TD_VC_100,         
 @ISCO_VEND   TD_VC_020,      
 @ISCO_LIQU      TD_VC_020,        
 @ISTI_FACT      TD_VC_003,        
 @ISCO_MODE_DIST TD_VC_003,        
 @ISST_ASTO_RESU TD_VC_001,  
@VSST_AGEN_PERC TD_VC_001,  
@VNPC_PERC_NAGP TD_NU_009_004,  
@VNPC_PERC_SAGP TD_NU_009_004,  
@VNPC_PERC_SAGR TD_NU_009_004,  
@VDFE_INIC_PERC TD_DT_001,  
@VNIM_MAXI_NPER TD_NU_016_004,  
@VSCO_MONE_NACI TD_VC_003,  
@VSST_PERC      TD_VC_001,  
@VNIM_PERC  TD_NU_016_004,  
@VNPC_PERC     TD_NU_009_004,  
@VSST_AGEN_PERC_CLIE TD_VC_001,  
@VSST_AGEN_RETE_CLIE TD_VC_001,  
@VNIM_TOTA_NACI TD_NU_016_004  
  
Begin Tran SP_TMDOCU_CLIE_I02     
  
Select   
@ISCO_EMPR = CO_EMPR,  
@ISCO_USUA_MODI = CO_USUA_MODI,  
@ISCO_TIPO_DOCU = CO_TIPO_DOCU,  
@ISNU_DOCU_CLIE = NU_DOCU_CLIE,  
@ISCO_CLIE = CO_CLIE,  
@ISNO_CLIE = NO_CLIE,  
@ISCO_MONE = CO_MONE,  
@ISCO_TIPO_OPER = CO_TIPO_OPER,  
@ISCO_COND_PAGO = CO_COND_PAGO,  
@IDFE_EMIS = FE_EMIS,  
@ISCO_UNID_CONC = CO_UNID_CONC,  
@IDFE_VENC = FE_VENC,  
@IDFE_PROG_COBR = FE_PROG_COBR,  
@IDFE_DESP = FE_DESP,  
@INFA_TIPO_CAMB = FA_TIPO_CAMB,  
@INIM_BRUT_AFEC = IM_BRUT_AFEC,  
@INIM_BRUT_INAF = IM_BRUT_INAF,  
@INIM_BRUT_EXON = IM_BRUT_EXON,  
@INIM_GAFI  = IM_GAFI,  
@INIM_MORA  = IM_MORA,  
@INIM_FLET  = IM_FLET,  
@ISST_AFEC_GAFI = ST_AFEC_GAFI,  
@ISST_AFEC_MORA = ST_AFEC_MORA,  
@ISST_AFEC_FLET = ST_AFEC_FLET,  
@ISST_DCT1_BRAF = ST_DCT1_BRAF,  
@ISST_DCT1_BRIN = ST_DCT1_BRIN,  
@ISST_DCT1_BREX = ST_DCT1_BREX,  
@ISST_DCT1_GAFI = ST_DCT1_GAFI,  
@ISST_DCT1_MORA = ST_DCT1_MORA,  
@ISST_DCT1_FLET = ST_DCT1_FLET,  
@ISST_DCT2_BRAF = ST_DCT2_BRAF,  
@ISST_DCT2_BRIN = ST_DCT2_BRIN,  
@ISST_DCT2_BREX = ST_DCT2_BREX,  
@ISST_DCT2_GAFI = ST_DCT2_GAFI,  
@ISST_DCT2_MORA = ST_DCT2_MORA,  
@ISST_DCT2_FLET = ST_DCT2_FLET,  
@INPC_DCT1 = PC_DCT1,  
@INIM_DCT1 = IM_DCT1,  
@INPC_DCT2 = PC_DCT2,  
@INIM_DCT2 = IM_DCT2,  
@ISCO_IMP1 = CO_IMP1,  
@INPC_IMP1 = PC_IMP1,  
@INIM_IMP1 = IM_IMP1,  
@ISCO_IMP2 = CO_IMP2,  
@INPC_IMP2 = PC_IMP2,  
@INIM_IMP2 = IM_IMP2,  
@ISCO_IMP3 = CO_IMP3,  
@INPC_IMP3 = PC_IMP3,  
@INIM_IMP3 = IM_IMP3,  
@INIM_TOTA = IM_TOTA,  
@INIM_PAGA_REND = IM_PAGA_REND,  
@ISCO_TIDO_ORIG = CO_TIDO_ORIG,  
@ISNU_DOCU_ORIG = NU_DOCU_ORIG,  
@IDFE_DOCU_ORIG = FE_DOCU_ORIG,  
@ISDE_OBSE = DE_OBSE,  
@ISCO_VEND = CO_VEND,  
@ISCO_LIQU = CO_LIQU,  
@ISTI_FACT = TI_FACT,  
@ISCO_MODE_DIST = CO_MODE_DIST,  
@ISST_ASTO_RESU = ST_ASTO_RESU  
From #TWDOCU_CLIE_I02    
        
 Select @VSST_INTE_BACH = ST_INTE_BACH,      
   @VSCO_TIPO_OPER = CO_TIPO_OPER,      
   @VSCO_TIPO_OPEG = CO_TIPO_OPEG,  
---OBTENGO DATOS DE PERCEPCION DE PARAMETROS DE OFITESO  
   @VSST_AGEN_PERC = ISNULL(ST_AGEN_PERC,'N'),  @VNPC_PERC_NAGP = ISNULL(PC_PERC_NAGP,0),  
   @VNPC_PERC_SAGP = ISNULL(PC_PERC_SAGP,0),  @VNPC_PERC_SAGR = ISNULL(PC_PERC_SAGR,0),  
   @VDFE_INIC_PERC = FE_INIC_PERC,  @VNIM_MAXI_NPER = ISNULL(IM_MAXI_NPER,0),  
   @VSCO_MONE_NACI = CO_MONE_DEFA      
 From TMPARA_TESO Where CO_EMPR = @ISCO_EMPR        
  
  Select @VNPC_PERC = 0, @VNIM_PERC = 0  
  
--CONVERCION A MONEDA NACIONAL  
IF @ISCO_MONE = @VSCO_MONE_NACI  
 Select @VNIM_TOTA_NACI = @INIM_TOTA  
ELSE  
    Select @VNIM_TOTA_NACI = @INIM_TOTA * @INFA_TIPO_CAMB  
  
--CONDICIONES QUE DEBE CUMPLIR EL DOUCMENTO PARA PODER CALCULAR EL PORCENTAJE DE PERCEPCION  
If ISNULL(@VSST_AGEN_PERC,'N') = 'S' AND @IDFE_EMIS >= @VDFE_INIC_PERC and @VNIM_TOTA_NACI  > @VNIM_MAXI_NPER   
   and ((@ISCO_IMP1 like 'IG%' and @INIM_IMP1>0) or (@ISCO_IMP2 like 'IG%' and @INIM_IMP2>0) or (@ISCO_IMP3 like 'IG%' and @INIM_IMP3>0))  
 Begin  
    Select @VSST_AGEN_PERC_CLIE = ISNULL(ST_PERC,'N'),   @VSST_AGEN_RETE_CLIE = ISNULL(ST_RETE,'N')  
    From   TMCLIE  
    Where  CO_CLIE = @ISCO_CLIE  
    And    CO_EMPR = @ISCO_EMPR  
  
 --DEVUELVE LOS VALORES QUE DETERMINAN SI EL DOC. ESTA SUJETO A PERCEPCION  
 If @VSST_AGEN_PERC_CLIE = 'S'    
  IF @VSST_AGEN_RETE_CLIE = 'S'  
    If @VNPC_PERC_SAGP > @VNPC_PERC_SAGR  
   Select @VNPC_PERC = @VNPC_PERC_SAGR  
    Else  
   Select @VNPC_PERC = @VNPC_PERC_SAGP  
  Else   
   Select @VNPC_PERC = @VNPC_PERC_SAGP  
 Else   
  IF @VSST_AGEN_RETE_CLIE = 'S'  
    If @VNPC_PERC_NAGP > @VNPC_PERC_SAGR  
   Select @VNPC_PERC = @VNPC_PERC_SAGR  
    Else  
   Select @VNPC_PERC = @VNPC_PERC_NAGP  
  Else   
    Select @VNPC_PERC = @VNPC_PERC_NAGP  
  
 Select @VNIM_PERC = round( (@VNPC_PERC/100.00) * IsNull(@INIM_TOTA,0),2)  
 End  
Else  
 begin  
  Select @VNPC_PERC = 0  
  Select @VNIM_PERC = 0  
 end  
  
If @VNIM_PERC > 0  
 Select @VSST_PERC = 'S'  
Else  
    Select @VSST_PERC = 'N'  
  
       
 If Not Exists(Select CO_EMPR      
    From   TMDOCU_CLIE         
    Where  CO_EMPR = @ISCO_EMPR        
    And CO_CLIE = @ISCO_CLIE        
    And CO_TIPO_DOCU = @ISCO_TIPO_DOCU        
    And NU_DOCU_CLIE = @ISNU_DOCU_CLIE)         
 Begin        
       
       
  /*Busco el ultimo correlativo usado para insertar el nro de comprobante*/         
  If @VSST_INTE_BACH = 'N' AND @ISST_ASTO_RESU = 'N' And ( @VSCO_TIPO_OPER != @ISCO_TIPO_OPER  And         
@VSCO_TIPO_OPEG != @ISCO_TIPO_OPER  )      
  Begin      
   EXEC SP_TTULTI_ASTO_Q01 @ISCO_EMPR,@ISCO_USUA_MODI, @ISCO_UNID_CONC, @ISCO_TIPO_OPER, @IDFE_EMIS,        
@VSNU_ASTO OUTPUT        
       
   If @@error <> 0        
   Begin        
    Rollback tran        
    Return        
   End            
  End      
      
       
  Insert Into TMDOCU_CLIE(        
  CO_EMPR, CO_TIPO_DOCU, NU_DOCU_CLIE, CO_CLIE, CO_MONE,         
  CO_TIPO_OPER, CO_COND_PAGO, FE_EMIS, CO_UNID_CONC, FE_VENC,         
  FE_PROG_COBR, FE_DESP, FA_TIPO_CAMB, IM_BRUT_AFEC, IM_BRUT_INAF,IM_BRUT_EXON,         
  IM_GAFI, IM_MORA, IM_FLET,ST_AFEC_GAFI, ST_AFEC_MORA, ST_AFEC_FLET,         
  ST_DCT1_BRAF, ST_DCT1_BRIN,ST_DCT1_BREX, ST_DCT1_GAFI, ST_DCT1_MORA, ST_DCT1_FLET,         
  ST_DCT2_BRAF, ST_DCT2_BRIN,ST_DCT2_BREX,ST_DCT2_GAFI, ST_DCT2_MORA, ST_DCT2_FLET,        
  PC_DCT1, IM_DCT1, PC_DCT2, IM_DCT2, CO_IMP1, PC_IMP1, IM_IMP1, CO_IMP2,         
  PC_IMP2, IM_IMP2, CO_IMP3,PC_IMP3, IM_IMP3, IM_TOTA, IM_PAGA, TI_DOCU_ORIG,         
  NU_DOCU_ORIG, FE_DOCU_ORIG, ST_CONT, DE_OBSE,CO_ESTA_DOCU, CO_USUA_CREA,FE_USUA_CREA, CO_USUA_MODI,        
  FE_USUA_MODI,         
  CO_VEND, CO_LIQU, TI_FACT, CO_MODE_DIST, TI_CLIE,       
  NU_ASTO,ST_PERC,PC_PERC)        
  Values  ( @ISCO_EMPR,  @ISCO_TIPO_DOCU, @ISNU_DOCU_CLIE, @ISCO_CLIE,        
  @ISCO_MONE, @ISCO_TIPO_OPER, @ISCO_COND_PAGO,  @IDFE_EMIS, @ISCO_UNID_CONC,         
  @IDFE_VENC, @IDFE_PROG_COBR, @IDFE_DESP, @INFA_TIPO_CAMB, @INIM_BRUT_AFEC,        
  @INIM_BRUT_INAF, @INIM_BRUT_EXON,@INIM_GAFI, @INIM_MORA, @INIM_FLET, @ISST_AFEC_GAFI,         
  @ISST_AFEC_MORA, @ISST_AFEC_FLET, @ISST_DCT1_BRAF, @ISST_DCT1_BRIN,@ISST_DCT1_BREX,         
  @ISST_DCT1_GAFI, @ISST_DCT1_MORA, @ISST_DCT1_FLET, @ISST_DCT2_BRAF,         
  @ISST_DCT2_BRIN,@ISST_DCT2_BREX, @ISST_DCT2_GAFI, @ISST_DCT2_MORA, @ISST_DCT2_FLET,         
  @INPC_DCT1, @INIM_DCT1, @INPC_DCT2, @INIM_DCT2, @ISCO_IMP1, @INPC_IMP1,         
  @INIM_IMP1, @ISCO_IMP2, @INPC_IMP2, @INIM_IMP2, @ISCO_IMP3, @INPC_IMP3,         
  @INIM_IMP3, @INIM_TOTA, @INIM_PAGA_REND, @ISCO_TIDO_ORIG, @ISNU_DOCU_ORIG,        
  @IDFE_DOCU_ORIG, 'N' , @ISDE_OBSE,'ACT', @ISCO_USUA_MODI,GETDATE(), @ISCO_USUA_MODI, GETDATE(),      
  @ISCO_VEND, @ISCO_LIQU, @ISTI_FACT, @ISCO_MODE_DIST, @VSTI_CLIE,       
  @VSNU_ASTO,@VSST_PERC,@VNPC_PERC)        
        
  If @@error <> 0        
  Begin        
   Rollback tran        
   Return        
  End            
       
  End        
 Else        
 Begin        
  Update  TMDOCU_CLIE        
  Set  CO_MONE = @ISCO_MONE,        
  CO_COND_PAGO = @ISCO_COND_PAGO,        
  FE_EMIS = @IDFE_EMIS,        
  FE_VENC = @IDFE_VENC,        
  FE_PROG_COBR = @IDFE_PROG_COBR,        
  FE_DESP = @IDFE_DESP,         
  FA_TIPO_CAMB = @INFA_TIPO_CAMB,        
  IM_BRUT_AFEC = @INIM_BRUT_AFEC,        
  IM_BRUT_INAF = @INIM_BRUT_INAF,     
  IM_BRUT_EXON = @INIM_BRUT_EXON,           
  IM_GAFI  = @INIM_GAFI,        
  IM_MORA = @INIM_MORA,        
  IM_FLET =  @INIM_FLET,        
  ST_AFEC_GAFI = @ISST_AFEC_GAFI,        
  ST_AFEC_MORA = @ISST_AFEC_MORA,        
  ST_AFEC_FLET = @ISST_AFEC_FLET,        
  ST_DCT1_BRAF = @ISST_DCT1_BRAF,        
  ST_DCT1_BRIN = @ISST_DCT1_BRIN,        
  ST_DCT1_BREX = @ISST_DCT1_BREX,        
ST_DCT1_GAFI = @ISST_DCT1_GAFI,        
  ST_DCT1_MORA = @ISST_DCT1_MORA,        
  ST_DCT1_FLET = @ISST_DCT1_FLET,        
  ST_DCT2_BRAF = @ISST_DCT2_BRAF,        
  ST_DCT2_BRIN = @ISST_DCT2_BRIN,    
  ST_DCT2_BREX = @ISST_DCT2_BREX,            
  ST_DCT2_GAFI = @ISST_DCT2_GAFI,        
  ST_DCT2_MORA = @ISST_DCT2_MORA,        
  ST_DCT2_FLET = @ISST_DCT2_FLET,        
  PC_DCT1 = @INPC_DCT1,        
  IM_DCT1 = @INIM_DCT1,        
  PC_DCT2 = @INPC_DCT2,        
  IM_DCT2 = @INIM_DCT2,        
  CO_IMP1 = @ISCO_IMP1,        
  PC_IMP1 = @INPC_IMP1,        
  IM_IMP1 = @INIM_IMP1,        
  CO_IMP2 = @ISCO_IMP2,        
  PC_IMP2 = @INPC_IMP2,        
  IM_IMP2 = @INIM_IMP2,        
  CO_IMP3 = @ISCO_IMP3,        
  PC_IMP3 = @INPC_IMP3,        
  IM_IMP3 = @INIM_IMP3,        
  IM_TOTA = @INIM_TOTA,        
  TI_DOCU_ORIG = @ISCO_TIDO_ORIG,        
  NU_DOCU_ORIG = @ISNU_DOCU_ORIG,        
  FE_DOCU_ORIG = @IDFE_DOCU_ORIG,        
  DE_OBSE =  RTRIM(@ISDE_OBSE),        
  CO_UNID_CONC  = @ISCO_UNID_CONC,        
  CO_VEND = @ISCO_VEND,        
  CO_LIQU = @ISCO_LIQU,        
  TI_FACT = @ISTI_FACT,                                   
  CO_USUA_MODI =  @ISCO_USUA_MODI,        
  FE_USUA_MODI  = Getdate() ,  
  ST_PERC  =  @VSST_PERC,   
  PC_PERC  =  @VNPC_PERC       
  Where   CO_EMPR = @ISCO_EMPR        
  And CO_CLIE = @ISCO_CLIE        
  And CO_TIPO_DOCU = @ISCO_TIPO_DOCU        
  And NU_DOCU_CLIE = @ISNU_DOCU_CLIE        
       
  If @@error <> 0        
  Begin        
   Rollback tran        
   Return        
  End            
       
 End                
       
        
Commit Tran        
        
/*-------------------------- Fin ------------------------------*/   
  
  
  
GO
